﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ActivitiesWindow.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Defines the ActivitiesWindow type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Dialogs
{
  #region Imported Namespaces


  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows.Forms;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the activities window.
  /// </summary>
  public partial class ActivitiesListDialog: ListDialogBase
  {
    #region Constants and Fields


    private readonly ActivityCollection _activityCollection;
    private readonly ActivityCategoryCollection _categories;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="ActivitiesListDialog"/> class.
    /// </summary>
    public ActivitiesListDialog(ActivityCollection activityCollection, ActivityCategoryCollection categories)
    {
      _activityCollection = activityCollection;
      _categories = categories;

      InitializeComponent();

      ListView = activitiesList;
      UpdateCloseMenuItem();
    }


    #endregion


    #region Methods


    /// <summary>
    /// Gets the list view item.
    /// </summary>
    /// <param name="activity">
    /// The activity.
    /// </param>
    /// <returns>
    /// </returns>
    protected override string GetListViewItemText(object activity)
    {
      return ((Activity)activity).Name;
    }


    /// <summary>
    /// Gets the sorted items list.
    /// </summary>
    /// <returns>
    /// </returns>
    protected override List<object> GetSortedItemsList()
    {
      var list = new List<Activity>(_activityCollection);
      list.Sort((activity1, activity2) => string.Compare(activity1.Name, activity2.Name));

      return new List<object>(list.Cast<object>());
    }


    /// <summary>
    /// Called when show check boxes flag changed.
    /// </summary>
    protected override void OnShowCheckBoxesChanged()
    {
      UpdateCloseMenuItem();
    }


    private void UpdateCloseMenuItem()
    {
      closeMenuItem.Text = ShowCheckBoxes ? "Cancel" : "Close";
    }


    /// <summary>
    /// Updates the controls.
    /// </summary>
    protected override void UpdateControls()
    {
      editMenuItem.Enabled = FocusedListViewItem != null;
      deleteMenuItem.Enabled = FocusedListViewItem != null;
    }


    private void AddActivityMenuItemClick(object sender, EventArgs e)
    {
      var dialog = new EditActivityDialog(null, _categories);
      dialog.Text = "Add Activity";

      ShowEditActivityDialog(dialog);

      if (dialog.DialogResult != DialogResult.OK)
      {
        return;
      }

      _activityCollection.Add(dialog.Activity);
      BuildListView();
    }


    private void CloseMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = ShowCheckBoxes ? DialogResult.Cancel : DialogResult.OK;

      Close();
    }


    private void DeleteMenuItemClick(object sender, EventArgs e)
    {
      var activity = (Activity)FocusedItem;

      DialogResult result =
        MessageBox.Show(string.Format("Are you sure you want to remove activity '{0}'?", activity.Name), 
                        "Remove Activity?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, 
                        MessageBoxDefaultButton.Button1);

      if (result != DialogResult.OK)
      {
        return;
      }

      _activityCollection.Remove(activity);
      RemoveItem(FocusedListViewItem);
    }


    private void EditActivityMenuItemClick(object sender, EventArgs e)
    {
      var activity = (Activity)FocusedItem;
      var dialog = new EditActivityDialog(activity, _categories);
      dialog.Text = "Edit Activity";

      ShowEditActivityDialog(dialog);

      if (dialog.DialogResult == DialogResult.OK)
      {
        BuildListView();
      }
    }


    private void ShowEditActivityDialog(EditActivityDialog dialog)
    {
      DialogResult result = dialog.ShowDialog();

      if (result != DialogResult.OK)
      {
        return;
      }

      Activity activity = dialog.Activity;

      var activities = new List<Activity>(_activityCollection.Where(collectionActivity => collectionActivity.Name == activity.Name));

      if (activities.Count <= 1)
      {
        return;
      }

      MessageBox.Show(string.Format("An activity with name '{0}' already exists.", activity.Name),
                      "Invalid Activity Name", MessageBoxButtons.OK, MessageBoxIcon.Hand,
                      MessageBoxDefaultButton.Button1);

      ShowEditActivityDialog(dialog);
    }


    #endregion
  }
}